<?php
declare (strict_types=1);

namespace app\exam\logic;

/**
 * 公共逻辑
 */
class Common
{

    protected $reg = '/<(audio|video)(.+?)src=\"(.+?)\" class=\"material-(audio|video)\">(.+?)<\/(audio|video)>/';

    /**
     * 解析并返回题干音视频附件
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-07
     *
     * @param  [type] $title [description]
     * @return array [type]        [description]
     */
    public function parseMaterial($str)
    {
        preg_match_all($this->reg, $str, $arr, PREG_SET_ORDER);
        $material = [];
        foreach ($arr as $v) {
            in_array($v[1], ['audio', 'video']) && $material[] = ['type' => $v[1], 'src' => $v[3]];
        }
        return $material;
    }

    /**
     * 移除音视频附件信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-07
     *
     * @param string $str
     * @return array|string|string[] [type]        [description]
     */
    public function removeMaterial(string $str = '')
    {
        preg_match_all($this->reg, $str, $arr, PREG_SET_ORDER);
        foreach ($arr as $v) {
            $str = str_replace($v[0], '', $str);
        }
        return $str;
    }

}
